package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.UUID;

/**
 * @Author: YangYu
 * @ClassName: DataService
 * @Date: Created in 16:31 2023/11/24
 * @Description: 数据转发
 */
@Service
public class DataService extends BaseService {
    @Resource
    private AsyncService asyncService;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;

    /**
     * @Author YangYu
     * @Description 水表项目用
     * @Date Created in 16:33 2023/11/24
     * @Param [jsonParam]
     */

    public void water_meter(JSONObject jsonParam){

        fireFightingiot(jsonParam, "data", "data", "data");
    }

    /**
     * @Author YangYu
     * @Description 风压监测系统（京电云）
     * @Date Created in 09:00 2024/4/3
     * @Param [jsonParam]
     */

    public RespEntity windPressure(JSONObject jsonParam) {
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("windPressure");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        try {
            //原始数据
            JSONObject payload = jsonParam.getJSONObject("payload");

            //网关ID
            String gatewayId = payload.getJSONObject("gwDeviceBo").getStr("serialNumber");
            //设备ID
            String serialNumber = payload.getStr("serialNumber");
            //模型数据
            JSONArray jsonArray = payload.getJSONArray("thingsModelSimpleItem");
            //风压
            Integer windPressure = 0;
            for (Object obj : jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                if (jsonObject.getStr("id").equals("wind_pressure")){
                    windPressure = jsonObject.getInt("value");
                }
            }
            // 时间
            String time = DateUtil.now();
            //组装数据
            JSONObject jsonParamN = new JSONObject();
            jsonParamN.put("number", gatewayId + serialNumber);
            //正常数据信息
            JSONObject jSONObject = new JSONObject();
            JSONArray jSONArray = new JSONArray();
            jSONObject.put("name", "风压");
            jSONObject.put("value", windPressure);
            jSONArray.add(jSONObject);
            //rest调用平时数据添加接口：
            jsonParamN.put("paraminfo", jSONArray);
            jsonParamN.put("time", time);
            //return RespEntity.success(jsonParamN);
            fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            jsonParamN.put("state","1");
            asyncService.updateDeviceCurrentState(jsonParamN);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number", gatewayId);
            jsonObject.put("state","1");
            asyncService.updateDeviceCurrentState(jsonObject);
            //大数据
            jsonParamN.put("dataType", "2");
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        }catch (Exception e){
            e.printStackTrace();
        }
        return RespEntity.success();
    }
}
